package org.acim.resource
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	/**
	 * 资源加载器
	 * @author Tide
	 */
	public class ResLoader extends EventDispatcher
	{
		public var resInfo:ResInfo;
		public var level:int;
		public var _loader:Loader;
		private var _isLoading:Boolean;
		
		private var _loaderContext:LoaderContext;
		
		public function ResLoader()
		{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.OPEN,onOpen);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
			
			_loaderContext = new LoaderContext(true);
		}
		
		//---------------------------------------------------------------
		// public function
		//---------------------------------------------------------------
		public function load(info:ResInfo):void
		{
			resInfo = info;
			level = resInfo.level;
			resInfo.isLoading = true;
			_loader.load(new URLRequest(resInfo.url),_loaderContext);//fp10以上，跨域加载资源需加LoaderContext
		}
		
		public function unload():void
		{
			_loader.unload();
		}
		
		public function close():void
		{
			if(!_isLoading)
			{
				return;
			}
			_isLoading = false;
			resInfo.isLoading = false;
			try
			{
				_loader.close();
			}
			catch(e:Error)
			{
				
			}
		}
		
		public function destroy():void
		{
			close();
			unload();
			resInfo = null;
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN,onOpen);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
		}
		
		//---------------------------------------------------------------
		// Event
		//---------------------------------------------------------------
		
		private function onOpen(event:Event):void
		{
			dispatchEvent(event);
		}
		
		private function onComplete(event:Event):void
		{
			dispatchEvent(event);
		}
		
		private function onProgress(event:ProgressEvent):void
		{
			dispatchEvent(event);
		}
		
		private function onError(event:IOErrorEvent):void
		{
			resInfo.isLoading = false;
			dispatchEvent(event);
			_isLoading = false;
		}
		
		//---------------------------------------------------------------
		// setter and getter
		//---------------------------------------------------------------
		
		public function get isLoading():Boolean
		{
			return _isLoading;
		}

		public function set isLoading(value:Boolean):void
		{
			_isLoading = value;
		}

	}
}